package com.itheima.Service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itheima.Service.ShoppingCartService;
import com.itheima.bean.ShoppingCart;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartDao shoppingCartDao;


    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        System.out.println("shoppingCart = " + shoppingCart);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if (shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }else{
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartInDB = shoppingCartDao.selectOne(lqw);
        if(cartInDB !=null){
            cartInDB.setNumber(cartInDB.getNumber()+1);
            shoppingCartDao.updateById(cartInDB);
            return cartInDB;
        }
            //设置用户ID
            shoppingCart.setUserId(BaseContext.getCurrentId());
            //设置添加时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //设置数量
            shoppingCart.setNumber(1);

            shoppingCartDao.insert(shoppingCart);


        return shoppingCart;
    }

    /**
     * 格局用户的ID来查询用户的购物车数据
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        return shoppingCartDao.selectList(lqw);
    }

    /**
     * 根据用户的ID来删除该用户的购物车数据
     *
     * @return
     */
    @Override
    public int clean() {
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        return shoppingCartDao.delete(lqw);
    }

    /**
     * 删减购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart delete(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if(shoppingCart.getDishId() !=null){
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartOne = shoppingCartDao.selectOne(lqw);
        if(cartOne.getNumber()!=1){
            cartOne.setNumber(cartOne.getNumber()-1);
            shoppingCartDao.update(cartOne,null);
            System.out.println("cartOne.getNumber() = " + cartOne.getNumber());
            return cartOne;
        }else{
            int row = shoppingCartDao.delete(lqw);
            //todo 提问下这里删除的思路
            return cartOne;
        }


    }
}
